package sisi.categories;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * CategoriesController.java - Controller class for the <i>categories database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 05/11/2013
 * 
 */

public class CategoriesController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Category[] getCategory(String cerca) {
		List<Category> categories = getCategoryList(cerca);
		return categories.toArray(new Category[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Category> getCategoryList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.categoryname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Category as c " + cWhere
							+ " order by c.categoryname");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Category> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<String> getCategoryListOnlyName() {

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c.categoryname from Category as c order by c.categoryname");
						
			List<String> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Category getCategoryxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Category categoryx = em.find(Category.class, id);
			return categoryx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Category addCategory(Category category) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(category);
			em.getTransaction().commit();
			category = refreshCategory(category);
		} finally {
			em.close();
			return category;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateCategory(Category category) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Category categoryx = em.find(Category.class, category.getCategoryid());
			categoryx.setCategoryid(category.getCategoryid());
			categoryx.setCategoryname(category.getCategoryname());
			categoryx.setDescription(category.getDescription());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Category refreshCategory(Category category) {

		Category categoryx = category;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			categoryx = em.find(Category.class, category.getCategoryid());
			em.flush();
			em.refresh(categoryx);
		} finally {
			em.close();
			return categoryx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeCategory(Category category) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Category categoryx = em.find(Category.class, category.getCategoryid());
			em.remove(categoryx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
